package com.optimatch.ui.view.customComponents.pages;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Label;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.MutableComboBoxModel;
import javax.swing.table.DefaultTableModel;

import com.optimatch.ui.model.Model;
import com.optimatch.ui.model.beans.ContrainteDeDate;


public class FixerMatchDatePanel extends JPanel implements Observer {

	JComboBox listeChoixJournees;
	JPanel listeMatchFixes;
	JPanel nouveauMatchFixe;
	JComboBox equipe1;
	JComboBox equipe2;
	JButton ajouterMatch;
	JCheckBox equipe1ADomicile;
	JTable table;
	JScrollPane scroll;

	
	public FixerMatchDatePanel() {
		super();
		init();
		addComponents();
	}

	private void init() {
		this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
		// init JCombo Boxes and other components
		equipe1 = new JComboBox();
		equipe2 = new JComboBox();
		
		// Ajout de la table
		listeMatchFixes = new JPanel();
		buildListeMatchFixes();
		
		// Ajout du formulaire
		nouveauMatchFixe = new JPanel();	
		buildNouveauMatchFixe();
		
	
		
	}
	
	private void addComponents() {
		this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
		this.add(listeMatchFixes);
		this.add(nouveauMatchFixe);
	}
	

	private void buildListeMatchFixes() {
		table = new JTable();
		listeMatchFixes.setLayout(new BoxLayout(listeMatchFixes,BoxLayout.Y_AXIS));
		listeMatchFixes.add(new Label("Match fixés"));
		table.setRowHeight(30);
		table.setBackground(new Color(217,226,243));
		scroll = new JScrollPane(table);
		scroll.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		listeMatchFixes.add(scroll);
	}
	
	private void buildNouveauMatchFixe() {
		nouveauMatchFixe.setLayout(new BorderLayout());
		nouveauMatchFixe.add(new Label("Ajouter un match fixe"),BorderLayout.NORTH);
		
		// Formulaire
		JPanel form = new JPanel();
		form.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		form.setLayout(new BoxLayout(form,BoxLayout.Y_AXIS));
		nouveauMatchFixe.add(form,BorderLayout.CENTER);

		
		// Ligne 0 du formulaire
		JPanel choixJournee = new JPanel();
		listeChoixJournees = new JComboBox();
		choixJournee.add(listeChoixJournees);
		form.add(choixJournee);
		
		// Premiere ligne du formulaire
		JPanel teams = new JPanel();
		teams.setLayout(new FlowLayout());
		teams.add(new Label("Rencontre"));
		teams.add(equipe1);
		teams.add(new Label("vs."));
		teams.add(equipe2);
		form.add(teams);

		
		// Deuxieme ligne du formulaire
	    equipe1ADomicile = new JCheckBox("Equipe 1 à domicile");
	    teams.add(equipe1ADomicile);
	    
	    // validation
	    JPanel buttonValidation = new JPanel();
	    ajouterMatch = new JButton();
	    buttonValidation.add(ajouterMatch);
	    form.add(buttonValidation);
	}

	public JComboBox getListeChoixJournees() {
		return listeChoixJournees;
	}

	public void setListeChoixJournees(JComboBox listeChoixJournees) {
		this.listeChoixJournees = listeChoixJournees;
	}
	

	public JComboBox getEquipe1() {
		return equipe1;
	}

	public JComboBox getEquipe2() {
		return equipe2;
	}

	
	public JButton getAjouterMatch() {
		return ajouterMatch;
	}
	

	public JCheckBox getEquipe1ADomicile() {
		return equipe1ADomicile;
	}

	public JTable getTable() {
		return table;
	}
	
	

	@Override
	public void update(Observable arg0, Object arg1) {
		Model model = (Model) arg0;
		int nbJournees = model.getProbleme().getNbJournes();
		int nbEquipes = model.getProbleme().getNbEquipes();
		
		// MAJ des JComboBox de journ�es
		MutableComboBoxModel boxModelJournees = new DefaultComboBoxModel();
		for (int i=0;i<nbJournees;i++) {
			boxModelJournees.addElement("Journée " + (i+1));
		}
		this.getListeChoixJournees().setModel(boxModelJournees);
	
		
		//MAJ des JComboBox d'equipes
		MutableComboBoxModel boxModelEquipe1 = new DefaultComboBoxModel();
		MutableComboBoxModel boxModelEquipe2 = new DefaultComboBoxModel();
		for (int i=0;i<nbEquipes;i++) {
			boxModelEquipe1.addElement(model.getProbleme().getListeEquipes().get(i).getName());
			boxModelEquipe2.addElement(model.getProbleme().getListeEquipes().get(i).getName());

		}
		this.getEquipe1().setModel(boxModelEquipe1);
		this.getEquipe2().setModel(boxModelEquipe2);
	

	}
	
	
}
